package com.zsl.swing.redis.desktop.window.dialog;

import com.zsl.swing.redis.desktop.common.IconPaths;
import com.zsl.swing.redis.desktop.model.OtherGeneralConfigEntity;
import com.zsl.swing.redis.desktop.type.BooleanEnum;
import com.zsl.swing.redis.desktop.type.BtnOptEnum;
import com.zsl.swing.redis.desktop.type.MainMenuEnum;
import com.zsl.swing.redis.desktop.utils.*;
import com.zsl.swing.redis.desktop.window.ZslRedisDesktopMainWindow;
import com.zsl.swing.redis.desktop.window.builder.BooleanComboBoxBuilder;
import com.zsl.swing.redis.desktop.window.builder.ConfirmCancelBtnPanelBuilder;
import com.zsl.swing.redis.desktop.window.ext.JIntegerField;
import com.zsl.swing.redis.desktop.window.ext.JLongField;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;

public class OtherGeneralConfigDialog extends JDialog {

    private final JComboBox<String> formattedDisplayField = BooleanComboBoxBuilder.getInstance();

    private final JIntegerField showAllCountField = new JIntegerField();

    private final JIntegerField queryLikeCountField = new JIntegerField();

    private final JIntegerField deleteLikeCountField = new JIntegerField();

    private final JLongField memoryLimitSupportField = new JLongField();

//    private JIntegerField logAreaField = new JIntegerField(COLUMNS);

//    private static final int COLUMNS = 300;

    private static final Image SET_ICON = IconUtils.getScaleImage(IconPaths.SET_ICON, 30, 30);


    public static void showDialog(){
        new OtherGeneralConfigDialog();
    }
    private OtherGeneralConfigDialog(){
        super(ZslRedisDesktopMainWindow.getMainWindow());

        this.setTitle(MainMenuEnum.OTHER_GENERAL_SET.getText());
        this.setIconImage(SET_ICON);
        int width = 800;
        int height = 600;
        this.setSize(width, height);

        int x = CommonUtils.maxWidth()/2 - width /2;
        int y = CommonUtils.maxHeight()/2 - height /2;
        this.setLocation(x, y);

        this.initDialog();

        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        this.setVisible(true);
    }

    private void initDialog() {
        JPanel propPanel = this.initPropPanel();
//        JPanel propPanel = this.initPropertyPanel();
        JPanel btnsPanel = this.initBtnPanel();

        Container contentPane = this.getContentPane();
        contentPane.setLayout(new BorderLayout());
        contentPane.add(propPanel,BorderLayout.CENTER);
//        contentPane.add(this.initPropertyBox(),BorderLayout.CENTER);
        contentPane.add(btnsPanel, BorderLayout.SOUTH);
    }

    private JPanel initBtnPanel() {
        ActionListener actionListener = event -> {
            String command = event.getActionCommand();
            if(BtnOptEnum.CANCEL.getText().equals(command)){
                this.dispose();
            }else if(BtnOptEnum.CONFIRM.getText().equals(command)){
                this.saveOtherGeneralConfig();
            }
        };

        return ConfirmCancelBtnPanelBuilder.getInstance(actionListener);
    }

    private OtherGeneralConfigEntity getEntity(){
        OtherGeneralConfigEntity entity = new OtherGeneralConfigEntity();
        String selectedItem = (String) this.formattedDisplayField.getSelectedItem();
        if(BooleanEnum.isTrueText(selectedItem)){
            entity.setFormattedDisplay(BooleanEnum.TRUE.getCode());
        }else{
            entity.setFormattedDisplay(BooleanEnum.FALSE.getCode());
        }

        entity.setShowALLScanCount(this.showAllCountField.getInteger());
        entity.setQueryLikeScanCount(this.queryLikeCountField.getInteger());
        entity.setDeleteLikeScanCount(this.deleteLikeCountField.getInteger());
        entity.setMemoryUsageLimit(this.memoryLimitSupportField.getLong());

        return entity;
    }

    private void setEntity(){
        OtherGeneralConfigEntity otherGeneralConfig = ZslRedisDesktopMainWindow.getOtherGeneralConfig();
        if(BooleanEnum.isTrue(otherGeneralConfig.getFormattedDisplay())){
            this.formattedDisplayField.setSelectedItem(BooleanEnum.TRUE.getText());
        }else{
            this.formattedDisplayField.setSelectedItem(BooleanEnum.FALSE.getText());
        }


        this.showAllCountField.setInteger(otherGeneralConfig.getShowALLScanCount());
        this.queryLikeCountField.setInteger(otherGeneralConfig.getQueryLikeScanCount());
        this.deleteLikeCountField.setInteger(otherGeneralConfig.getDeleteLikeScanCount());
        this.memoryLimitSupportField.setLong(otherGeneralConfig.getMemoryUsageLimit());
    }

    private void saveOtherGeneralConfig() {
        OtherGeneralConfigEntity entity = this.getEntity();

        OtherGeneralConfigEntity defaultConfig = ZslRedisDesktopMainWindow.getOtherGeneralConfig();
        if(entity.getShowALLScanCount() <= 0){
            entity.setShowALLScanCount(defaultConfig.getShowALLScanCount());
        }

        if(entity.getQueryLikeScanCount() <=0 ){
            entity.setQueryLikeScanCount(defaultConfig.getQueryLikeScanCount());
        }

        if(entity.getDeleteLikeScanCount() <= 0){
            entity.setDeleteLikeScanCount(defaultConfig.getDeleteLikeScanCount());
        }

        if(entity.getMemoryUsageLimit() <= 0L){
            entity.setMemoryUsageLimit(defaultConfig.getMemoryUsageLimit());
        }

        boolean result = ConfigUtils.writeOtherGeneralConfig(entity);
        if(result){
            this.dispose();
            DialogUtils.restartConfirmDialog(ZslRedisDesktopMainWindow.getMainWindow(), "保存设置成功，重启后生效！");
        }else{
            DialogUtils.errorDialog(ZslRedisDesktopMainWindow.getMainWindow(),"保存设置失败！");
        }
    }


    private JPanel initPropPanel(){
        JPanel panel = new JPanel(new GridBagLayout());

        int labelWidth = 1, height = 1, fieldWidth = 2, separator = 1, next = height + separator;
        GridBagUtils.getInstance()
                .add(new JLabel("命令行窗口中是否格式化展示结果"),panel,0,0, labelWidth, height)
                .add(this.formattedDisplayField,panel,0,next, fieldWidth, height)
                .add(new JLabel("展示所有key时每页数量"),panel, 0, 2*next, labelWidth,height)
                .add(this.showAllCountField,panel,0,3*next, fieldWidth, height)
                .add(new JLabel("模糊查询时每次查询数量"),panel,0, 4*next,labelWidth,height)
                .add(this.queryLikeCountField,panel,0,5*next,fieldWidth,height)
                .add(new JLabel("模糊删除时每次查询数量"),panel,0, 6*next, labelWidth, height)
                .add(this.deleteLikeCountField,panel,0,7*next,fieldWidth,height)
                .add(new JLabel("界面支持的key展示最大内存限制(byte)"),panel,0,8*next,labelWidth,height)
                .add(this.memoryLimitSupportField,panel,0, 9*next, fieldWidth, height);


        this.setEntity();

        this.formattedDisplayField.setEditable(false);
        return panel;
    }

    private JPanel initPropertyPanel() {
        JPanel panel = new JPanel(new GridBagLayout());

        int start = 0,labelWidth = 1, height = 1, fieldWidth = 3, separator = 1;
        GridBagUtils.getInstance()
                .add(new JLabel("展示值时是否格式化展示："),panel, start, start, labelWidth,height)
                .add(this.formattedDisplayField,panel,labelWidth + separator, start, fieldWidth, height)
                .add(new JLabel("展示所有时每页展示数量："), panel, start, height + separator, labelWidth, height)
                .add(this.showAllCountField, panel, labelWidth + separator, height + separator, fieldWidth, height)
                .add(new JLabel("模糊查询时每次查询数量："), panel, start, 2*(height + separator), labelWidth, height)
                .add(this.queryLikeCountField, panel, labelWidth + separator, 2*(height + separator), fieldWidth, height)
                .add(new JLabel("模糊删除时每次查询数量："), panel, start, 3*(height + separator), labelWidth, height)
                .add(this.deleteLikeCountField, panel, labelWidth + separator, 3*(height + separator), fieldWidth, height)
                .add(new JLabel("界面支持的最大字符长度："), panel, start, 4*(height + separator), labelWidth, height)
                .add(this.memoryLimitSupportField, panel, labelWidth + separator, 4*(height + separator), fieldWidth, height);
//                .add(new JLabel("日志"), panel, start, 5*(height + separator), labelWidth, height);
//                .add(this.logAreaField, panel, labelWidth + separator, 5*(height + separator), fieldWidth, height);

        this.setEntity();

        this.formattedDisplayField.setEditable(false);
        return panel;
    }


}
